package Dao;

import pojo.Car;
import pojo.Record;
import pojo.User;
/**
 * 租车记录初始化
 */
import java.io.*;
import java.util.ArrayList;

public class RecordDao {
    private static ArrayList<Record> records=new ArrayList<>();
    private static File file=new File(FileLocation.file+"\\Records.txt");
    static {
        records.add(new Record(0,new User(),new Car()));
    }
    /**
     * 反序列化对象
     * @return
     */
    public static ArrayList<Record> getRecords() {
        FileInputStream input=null;
        ObjectInputStream oInput = null;
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            input = new FileInputStream(file);
            oInput = new ObjectInputStream(input);
            records= (ArrayList<Record>) oInput.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                oInput.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return records;
    }

    /**
     * 序列化对象
     */
    public static void setRecords() {
        FileOutputStream input=null;
        ObjectOutput oOut=null;
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            input = new FileOutputStream(file);
            oOut = new ObjectOutputStream(input);
            oOut.writeObject(records);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oOut.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
